//
//  YCVisitorView.swift
//  WeiboDemo
//
//  Created by 尹成 on 2017/1/10.
//  Copyright © 2017年 yc. All rights reserved.
//

import UIKit

class YCVisitorView: UIView {
    
    /// 使用字典设置访客视图信息
    ///
    /// - Parameter dict: [imageName/message]
    ///如果是首页,imageName == ""
    var visitorInfo:[String:String]?{
        didSet{
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            tipLable.text = message
            if imageName == "" {
                startAnimation()
                return
            }
            
            iconMaskView.isHidden = true
            houseView.isHidden = true
            iconView.image = UIImage(named: imageName)
        }
    }
    
    //首页选装动画
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2*M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        //动画完成不删除，除非iconview被释放
        //在设置连续播放的动画时非常有用
        anim.isRemovedOnCompletion = false
        //将动画添加到图层
        iconView.layer.add(anim, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    fileprivate lazy var iconMaskView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    fileprivate lazy var houseView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    fileprivate lazy var tipLable:UILabel = UILabel.cz_label(withText: "关注一些人,回这里看看有什么惊喜",
                                                             fontSize: 13,
                                                             color: UIColor.gray)
    lazy var registerButton:UIButton = UIButton.cz_textButton("注册",
                                                                          fontSize: 16,
                                                                          normalColor: UIColor.orange,
                                                                          highlightedColor: UIColor.black,
                                                                          backgroudImageName:"common_button_white_disable")
    
    lazy var loginButton:UIButton = UIButton.cz_textButton("登录",
                                                                       fontSize: 16,
                                                                       normalColor: UIColor.darkGray,
                                                                       highlightedColor: UIColor.black,
                                                                       backgroudImageName:"common_button_white_disable")
}


// MARK: - 设置界面
private extension YCVisitorView{
    func setupUI() {
        //开发过程中,能使用颜色解决的不要用图像,颜色效率高
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        addSubview(iconView)
        addSubview(iconMaskView)
        addSubview(houseView)
        addSubview(tipLable)
        tipLable.textAlignment = NSTextAlignment.center
        addSubview(registerButton)
        addSubview(loginButton)
        
        //源生布局
        //1.取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        let lineSpace:CGFloat = 20.0
        //2.自动布局
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 60))
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: -60))
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: lineSpace))
        
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: lineSpace))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 72))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 35))
        
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .top,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .width,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .height,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //使用VFL添加遮罩
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[iconMaskView]-0-|",
                                                      options: [],
                                                      metrics: nil,
                                                      views: ["iconMaskView":iconMaskView]))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[iconMaskView]-(spacing)-[registerButton]",
                                                      options: [],
                                                      metrics: ["spacing":-20],
                                                      views: ["iconMaskView":iconMaskView,"registerButton":registerButton]))
    }
}
